package com.distribute.client.service;

import com.distribute.client.cache.DemoConcurrentMapCache;
import com.distribute.client.entity.CategoryEntity;
import com.distribute.core.core.el.DLCUpdateNotify;
import com.distribute.core.core.el.ExpressionEvaluator;
import com.distribute.core.notify.DLCNotify;
import org.springframework.context.expression.AnnotatedElementKey;
import org.springframework.expression.EvaluationContext;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * TODO
 *
 * @author chennengcheng
 * @date 2021-09-15 17:23
 * @remark
 */
@Service
public class DemoService {

    @Resource
    private DemoConcurrentMapCache demoConcurrentMapCache;

    @Resource
    private DLCNotify notifyCacheUpdateImpl;

    public static int z = 1;

    // 注解刷新
    @DLCUpdateNotify(modelPrefix = "product", model = {"category", "categoryFont"}, key = "#result.result")
    public CategoryEntity updateDemo(String id){
//        redisNotifyMessage.setData();
//        String value = demoConcurrentMapCache.getValueByKey(id, () -> getById(id));
//        String valueByKey = demoConcurrentMapCache.getValueByKey(id, () -> getById(id), v -> v = v + "1111");
        CategoryEntity categoryEntity = new CategoryEntity();
        categoryEntity.setAge("131");
        categoryEntity.setName("#@313123131");
        categoryEntity.setResult("3213131低订单");
        return categoryEntity;
    }

    // 手动刷新
    public void updateDemo2(String id){
        notifyCacheUpdateImpl.notifyCacheUpdate("product", "demoCache", id);
    }


    public String getById(String id){

        return "";
    }
    // 收到通知，直接刷新所有缓存
//
//    public void getData(int z){
//        ConcurrentMap<String, String> cache = demoGuavaLoadingCache.getCache();
//        String s = cache.get("缓存key+1" + z);
//        System.out.println("##### " + s);
//    }

    public static void main(String[] args) throws NoSuchMethodException {
        DemoService demoService = new DemoService();
        ExpressionEvaluator<String> evaluator = new ExpressionEvaluator<>();
        EvaluationContext evaluationContext = evaluator.createEvaluationContext(
                demoService,
                DemoService.class,
                DemoService.class.getDeclaredMethod("updateDemo", String.class),
                new Object[]{"123"});
        AnnotatedElementKey methodKey = new AnnotatedElementKey(DemoService.class.getDeclaredMethod("updateDemo", String.class), DemoService.class);
        String condition = evaluator.condition("#result", methodKey, evaluationContext, String.class);
        System.out.println(condition);

//        CacheOperationExpressionEvaluator evaluatorResult = new CacheOperationExpressionEvaluator();
//
//        EvaluationContext evaluationContextResult = evaluatorResult.createEvaluationContext(
//                DemoService.class.getDeclaredMethod("updateDemo", String.class),
//                new Object[]{"123"},
//                demoService,
//                DemoService.class
//               );
//        AnnotatedElementKey methodKey = new AnnotatedElementKey(DemoService.class.getDeclaredMethod("updateDemo", String.class), DemoService.class);
//        String condition = evaluator.condition("#result", methodKey, evaluationContext, String.class);
//        System.out.println(condition);
    }
}
